package com.checknull.druineage.util;

import java.lang.reflect.Field;

public class ClassUtils {

    public interface IObjectHandle {

        void handle(Object object);

    }

    /**
     * 遍历类的每个属性
     *
     * @param clazz        类
     * @param object       实例
     * @param objectHandle 回调
     */
    public static void loopField(Class clazz, Object object, IObjectHandle objectHandle) {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (field.getName().equalsIgnoreCase("parent") || field.getName().equalsIgnoreCase("serialVersionUID")) {
                continue;
            }
            Object o = null;
            try {
                o = field.get(object);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (o == null || o.equals(String.class)) {
                continue;
            }
            objectHandle.handle(o);
        }
    }

}
